#include "app_config.h"
#include "asm/charge.h"
#include "asm/pwm_led.h"
#include "system/event.h"
#include "system/app_core.h"
#include "system/includes.h"
#include "app_action.h"
#include "asm/wdt.h"
#include "app_main.h"
#include "app_power_manage.h"
#include "app_handshake.h"

#define LOG_TAG_CONST       APP_CHARGE
#define LOG_TAG             "[APP_CHARGE]"
#define LOG_ERROR_ENABLE
#define LOG_DEBUG_ENABLE
#define LOG_INFO_ENABLE
/* #define LOG_DUMP_ENABLE */
#define LOG_CLI_ENABLE
#include "debug.h"

#if TCFG_CHARGE_ENABLE

#if TCFG_HANDSHAKE_ENABLE
static void handshake_complete(void)
{
    handshake_app_stop();
    if (get_lvcmp_det()) {
        charge_start();
    }
}
#endif

void charge_start_deal(void)
{
    log_info("%s\n", __func__);
    power_set_mode(PWR_LDO15);
}

void ldo5v_keep_deal(void)
{
    log_info("%s\n", __func__);
#if TCFG_HANDSHAKE_ENABLE
    handshake_app_start(2, handshake_complete);
#endif
}

void charge_full_deal(void)
{
    log_info("%s\n", __func__);
    charge_close();
}

void charge_close_deal(void)
{
    log_info("%s\n", __FUNCTION__);
}

void charge_ldo5v_in_deal(void)
{
    log_info("%s\n", __FUNCTION__);
#if TCFG_HANDSHAKE_ENABLE
    handshake_app_start(2, handshake_complete);
#else
    charge_start();
#endif
/*
    struct application *app = get_current_app();
    if (app) {
        if (strcmp(app->name, "idle")) {
            app_switch("idle", ACTION_IDLE_MAIN);
        }
    }
    */
}

void charge_ldo5v_off_deal(void)
{
    log_info("%s\n", __FUNCTION__);
#if TCFG_HANDSHAKE_ENABLE
    handshake_app_stop();
#endif
    charge_close();
    power_set_mode(TCFG_LOWPOWER_POWER_SEL);
#if TCFG_SYS_LVD_EN
    vbat_check_init();
#endif
/*
    struct application *app = get_current_app();
    if (app) {
        if (strcmp(app->name, "idle") == 0) {
            power_set_soft_poweroff(); //��?3?3?��????��?��1??��
            //?1��?��??��?a?����?D����a��?DD�̡�??
        }
    }
    */
}
u16 charge_led_timer = 0;
u16 charge_full_timer = 0;
extern u8 my_app_flag;
extern void charge_led_hander();
extern void charge_led_off();
extern void charge_led_full();
int app_charge_event_handler(struct device_event *dev)
{
    switch (dev->event) {
    case CHARGE_EVENT_CHARGE_START:
        charge_start_deal();
        if (charge_led_timer == 0)
            charge_led_timer = sys_timer_add(NULL, charge_led_hander, 1000);

        if (charge_full_timer) {
            sys_timer_del(charge_full_timer);
            charge_full_timer = 0;
        }
        my_app_flag = 1;
        //printf(">>>>>>>>CHARGE_EVENT_CHARGE_START\n");
        break;
    case CHARGE_EVENT_CHARGE_CLOSE:
        charge_close_deal();
        if (charge_led_timer) {
            sys_timer_del(charge_led_timer);
            charge_led_timer = 0;
        }
        // charge_led_off();
        // my_app_flag = 0;
        //printf(">>>>>>>>CHARGE_EVENT_CHARGE_CLOSE\n");
        break;
    case CHARGE_EVENT_CHARGE_FULL:
        charge_full_deal();
        if (charge_led_timer) {
            sys_timer_del(charge_led_timer);
            charge_led_timer = 0;
        }
        if (charge_full_timer == 0)
            charge_full_timer = sys_timer_add(NULL, charge_led_full, 2000);
        my_app_flag = 5;
        //printf(">>>>>>>>CHARGE_EVENT_CHARGE_FULL\n");
        break;
    case CHARGE_EVENT_LDO5V_KEEP:
        ldo5v_keep_deal();
        break;
    case CHARGE_EVENT_LDO5V_IN:
        charge_ldo5v_in_deal();
        break;
    case CHARGE_EVENT_LDO5V_OFF:
        charge_ldo5v_off_deal();
        if (charge_full_timer) {
            sys_timer_del(charge_full_timer);
            charge_full_timer = 0;
        }
        charge_led_off();
        my_app_flag = 0;
        break;
    default:
        break;
    }
    return 0;
}

#endif

